package com.lanjiyin.cloud.common.util.security;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;
/**
 * MD5工具类
 * @author tudaojun
 *
 */
public class MD5 {
	public static String md5(String str) {
		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
				'a', 'b', 'c', 'd', 'e', 'f' };
		try {
			byte[] bytes = str.getBytes();
			MessageDigest messageDigest = MessageDigest.getInstance("MD5");
			messageDigest.update(bytes);
			byte[] updateBytes = messageDigest.digest();
			int len = updateBytes.length;
			char myChar[] = new char[len * 2];
			int k = 0;
			for (int i = 0; i < len; i++) {
				byte byte0 = updateBytes[i];
				myChar[k++] = hexDigits[byte0 >>> 4 & 0x0f];
				myChar[k++] = hexDigits[byte0 & 0x0f];
			}
			return new String(myChar);
		} catch (Exception e) {
			return null;
		}
	}
	/**
	 * MD5加盐
	 * @param pwd
	 * @return      
	 * String
	 */
	public static String getSaltMd5AndSha(String pwd) {
		Random random = new Random();
        StringBuilder sBuilder = new StringBuilder(8);
        sBuilder.append(random.nextInt(9999)).append(random.nextInt(9999));
        int len = sBuilder.length();
        if (len < 8) {
              for (int i = 0; i < 8 - len; i++) {
                sBuilder.append("0");
            }
        }
        // 生成最终的加密盐
        String salt = sBuilder.toString();
        pwd = md5AndSha(pwd + salt);
        char[] cs = new char[24];
        for (int i = 0; i < 24; i += 3) {
            cs[i] = pwd.charAt(i / 3 * 2);
            char c = salt.charAt(i / 3);
            cs[i + 1] = c;
            cs[i + 2] = pwd.charAt(i / 3 * 2 + 1);
        }
        return String.valueOf(cs);
	}
	/**
     * md5和sha-1混合加密
     *
     * @param inputText 要加密的内容
     *
     * @return String md5和sha-1混合加密之后的密码
     */
     public static String md5AndSha(String inputText) {
         return sha(md5n(inputText));
     }
 
 
   /**
     * md5加密
     *
     * @param inputText 要加密的内容
     *
     * @return String  md5加密之后的密码
     */
     public static String md5n(String inputText) {
	     return encrypt(inputText, "md5");
     }
 
 
   /**
     * sha-1加密
     *
     * @param inputText  要加密的内容
     *
     * @return  sha-1加密之后的密码
     */
     public static String sha(String inputText) {
         return encrypt(inputText, "sha-1");
     }
     /**
      * md5或者sha-1加密
      * 
      * @param inputText   要加密的内容
      *           
      * @param algorithmName  加密算法名称：md5或者sha-1，不区分大小写
      *            
      * @return String  md5或者sha-1加密之后的结果
      */
      private static String encrypt(String inputText, String algorithmName) {
 	     if (inputText == null || "".equals(inputText.trim())) {
 		     throw new IllegalArgumentException("请输入要加密的内容");
 	     }
 	     if (algorithmName == null || "".equals(algorithmName.trim())) {
 		     algorithmName = "md5";
 	     }
 	     String encryptText = null;
 	     try {
   		     MessageDigest m = MessageDigest.getInstance(algorithmName);
 		     m.update(inputText.getBytes("UTF8"));
 		     byte s[] = m.digest();
 		     return hex(s);
 	     } catch (NoSuchAlgorithmException e) {
 		     e.printStackTrace();
 	     } catch (UnsupportedEncodingException e) {
 		     e.printStackTrace();
 	     }
 	     return encryptText;
      }
      /**
       * byte[]字节数组 转换成 十六进制字符串
       * 
       * @param arr 要转换的byte[]字节数组
       *
       * @return  String 返回十六进制字符串
       */
       private static String hex(byte[] arr) {
  	     StringBuffer sb = new StringBuffer();
  	     for (int i = 0; i < arr.length; ++i) {
  		     sb.append(Integer.toHexString((arr[i] & 0xFF) | 0x100).substring(1, 3));
  	     }
  	     return sb.toString();
       }
}
